<?php

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Foundation\Application;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

//    /**
//     * 首页
//     * Author Zhenzh
//     * DateTime: 2019/8/7 15:47
//     *
//     * @ApiTitle    (首页)
//     * @ApiSummary  (描述)
//     * @ApiBody     (Body)
//     * @ApiMethod   (POST)
//     * @ApiRoute    (/api/index/index)
//     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
//     * @ApiParams   (name="name", type="string", required=true, description="description", sample="")
//     * @ApiParams   (name="name", type="string", required=true, description="description", sample="")
//     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
//     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
//     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
//     * @ApiReturn ()
//     */
//    public function index()
//    {
//        $data = [
//            '/uploads/images/20110412100351306.jpg',
//            '/uploads/images/20110412100431955.jpg',
//            '/uploads/images/20110412100453447.jpg',
//            '/uploads/images/20110412100518269.jpg',
//            '/uploads/images/20110412100542518.jpg',
//            '/uploads/images/20111014161542266.jpg',
//        ];
//        $this->success('请求成功', $data);
//    }

    /**
     * @ApiTitle    (首页轮播图)
     * @ApiAuthor   (Zhenzh)
     * @ApiDateTime (2019/8/16 9:54)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/slides)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function slides()
    {
        $data = [
            '/uploads/images/20110412100351306.jpg',
            '/uploads/images/20110412100431955.jpg',
            '/uploads/images/20110412100453447.jpg',
            '/uploads/images/20110412100518269.jpg',
            '/uploads/images/20110412100542518.jpg',
            '/uploads/images/20111014161542266.jpg',
        ];
        $this->success('', $data);
    }

    /**
     * @ApiTitle    (获取公众号永久素材)
     * @ApiAuthor   (Zhenzh)
     * @ApiDateTime (2019/8/7 15:47)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/materials)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="type", type="string", required=false, description="素材的类型，图片（image）、视频（video）、语音 （voice）、图文（news）", sample="")
     * @ApiParams   (name="offset", type="int", required=false, description="从全部素材的该偏移位置开始返回，可选，默认 0，0 表示从第一个素材 返回", sample="")
     * @ApiParams   (name="count", type="int", required=false, description="返回素材的数量，可选，默认 20, 取值在 1 到 20 之间", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function materials()
    {
        $cache_key = 'materials';
        $data = cache($cache_key);

        $debug = $this->request->request('debug', 0);
        if ($debug && $data) {
            dd($data);
        }

        if (!$data) {
            $type = $this->request->post('type', 'news');
            $offset = $this->request->post('offset', 0);
            $count = $this->request->post('count', 5);

            $app = new Application(get_addon_config('wechat'));
            $material = $app->material;

            $materials = $material->lists($type, $offset, $count);

            $data =[];
            $data['item_count'] = $materials['item_count'];
            $data['total_count'] = $materials['total_count'];
            if (isset($materials['item'])) {

                foreach ($materials['item'] as $key => $material) {

                    $item['media_id'] = $material['media_id'];
                    if ($material['content']) {
                        $item['content']['create_time'] = $material['content']['create_time'];
                        $item['content']['update_time'] = $material['content']['update_time'];

                        foreach ($material['content']['news_item'] as $k => $news) {

                            $item['content']['news_item'][] = [
                                'title' => $news['title'],
                                'digest' => $news['digest'],
                                'content_source_url' => $news['content_source_url'],
                                'show_cover_pic' => $news['show_cover_pic'],
                                'url' => $news['url'],
                                'thumb_media_id' => $news['thumb_media_id'],
                                'thumb_url' => $news['thumb_url'],
                            ];

                        }
                    }


                    $data['item'][] = $item;
                }
            }

            cache($cache_key, $data, 60);
        }

        $this->success('', $data);
    }

    /**
     * @ApiTitle    (获取公众号指定素材)
     * @ApiAuthor   (Zhenzh)
     * @ApiDateTime (2019/8/7 15:47)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/material)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiParams   (name="media_id", type="string", required=true, description="media_id", sample="")
     * @ApiParams   (name="thumb_media_id", type="string", required=false, description="thumb_media_id", sample="")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function material()
    {
        $media_id = $this->request->request('media_id');
        $thumb_media_id = $this->request->request('thumb_media_id');

        if (!$thumb_media_id)
            $this->error('fail');

        $app = new Application(get_addon_config('wechat'));
        $material = $app->material;

        $material = $material->get($media_id);

        $data = [];
        if ($thumb_media_id) {
            $data['create_time'] = $material['create_time'];
            $data['update_time'] = $material['update_time'];
            foreach ($material['news_item'] as $index => $item) {
                if ($thumb_media_id == $item['thumb_media_id']) {
                    $data['news_item'][] = $item;
                    break;
                }
            }
        } else {
            $data = $material;
        }

        $this->success('', $data);

    }


}
